/*
题目描述：反转单词顺序
 */
public class E58_1 {
    public static void main(String[] args) {
        String str = "I am a student.";
        String res = ReverseSentence(str);
        System.out.println(res);
    }

    public static String ReverseSentence(String str) {
        if(str.trim().equals("")){      //需要注意去掉前后是空格的情况，不然split的时候会出现问题！！
            return str;
        }
        String[] strs = str.split(" ");
        StringBuilder sb = new StringBuilder();
        for(int i = strs.length - 1; i >= 0; i--){
            if(!strs[i].equals("")){
                sb.append(strs[i] + " ");
            }
        }
        return sb.substring(0, sb.length() - 1);
    }
}
